           
#!/usr/bin/env phyton
#-*- coding: utf-8 -*-

import math
import pickle
import os
import sqlite3
import csv
import sys





def iniciar_db (x_times):

    fileCSV = open('reb{0}.csv'.format(x_times), 'rb')
    spamReader = csv.reader( fileCSV, delimiter = ',')

    conn = sqlite3.connect("teste.db")
    c = conn.cursor()
    
    if x_times == 0:
        conn.execute (''' drop table if exists docentes''')
        conn.execute('''create table if not exists docentes (cod_docente integer, nome text, ultimo_grau text, curso text, categoria text, regime text, ano text,
                       tipo_estabelecimento text, nome_establecimento text)''')
        conn.commit()
        pass
    
    spamReader.next()

    for row in spamReader:
        
    
        conn.execute(''' insert into docentes values ("{0}", "{1}", "{2}", "{3}", "{4}", "{5}", "{6}", "{7}", "{8}")'''.\
                  format(row[0].replace('"', ' '), row[1].replace('"', ' '), row[2].replace('"', ' '), row[3].replace('"', ' '), row[4].replace('"', ' ') \
                         , row[5].replace('"', ' '), row[6].replace('"', ' '), row[7], row[8].replace('"', ' ')))
        pass
    
    fileCSV.close()
    conn.commit()
    conn.close()
           
    pass

def criar_db():
    for i in range (0, 10):
        iniciar_db(i)
        print 'sucesso'
        pass
    pass
        
def lista_db():
    conn = sqlite3.connect("teste.db")
    c = conn.cursor()

    c.execute(''' select * from docentes ''')
    lista = []
    for x in c.fetchall():
        lista.append(x)
        pass
    conn.close()           
    return lista

def estatistica1(lista_db):
    list_test = []
    compare = []
    for row in lista_db:
         print row
         pass
    pass

